package cn.wolfcode.shop.service.impl;

import cn.wolfcode.shop.domain.*;
import cn.wolfcode.shop.mapper.*;
import cn.wolfcode.shop.service.IPropertyService;
import cn.wolfcode.shop.vo.ProductVo;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class PropertyServiceImpl implements IPropertyService {
    @Autowired
    private PropertyMapper propertyMapper;
    @Autowired
    private CatalogMapper catalogMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductImageMapper productImageMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapperl;
    @Autowired
    private ProductPropertyValueMapper productPropertyValueMapper;


    /**
     * 商品属性列表
     * @param catalogId
     * @return
     */
    @Override
    public List<Property> selectByCatalogId(Long catalogId) {
        return propertyMapper.selectByCatalogId( catalogId) ;
    }

    /**
     * 商品分类属性编辑
     * @param property
     */
    @Override
    public void saveOrUpdateProperty(Property property) {
        if(property.getId()!=null){
            propertyMapper.updateByPrimaryKey(property);
        }else {
            propertyMapper.insert(property);
        }
    }

    /**
     * 删除商品属性
     * @param id
     */
    @Override
    public void propertyDelete(Long id) {
        propertyMapper.deleteByPrimaryKey(id);
    }

    /**
     * 商品录入保存
     * @param productVo
     */
    @Override
    public void save(ProductVo productVo) {
        //获取商品对象
        Product product = productVo.getProduct();
        product.setCreatedDate(new Date());
        product.setLastModifiedDate(new Date());
        int insert = productMapper.insert(product);
        if(insert != 0){
            //保存商品明细
            ProductDetails productDetails = productVo.getProductDetails();
            productDetails.setProductId(product.getId());
            productDetailsMapperl.insert(productDetails);
            //保存商品相册表
            List<ProductImage> productImagesList = productVo.getProductImagesList();
            String imagePath = productImagesList.get(0).getImagePath();
            product.setImage(imagePath);
            productMapper.updateByPrimaryKey(product);


            for (ProductImage productImage : productImagesList) {
                if(productImage.getImagePath() != null && productImage.getImagePath().trim().length() >0){

                    productImage.setProductId(productVo.getProduct().getId());
                    productImageMapper.insert(productImage);
                }
            }
            //保存属性值
            List<ProductPropertyValue> productPropertyValueList = productVo.getProductPropertyValueList();
            for (ProductPropertyValue productPropertyValue : productPropertyValueList) {
                productPropertyValue.setProductId(productVo.getProduct().getId());
                productPropertyValueMapper.insert(productPropertyValue);
            }
        }


    }
}
